﻿Public Class Color
    Inherits Result

    <System.Xml.Serialization.XmlElement("hex")>
    Public Property HTMLColorCode As String

    <System.Xml.Serialization.XmlElement("rgb")>
    Public Property RGB As RGB

    <System.Xml.Serialization.XmlElement("hsv")>
    Public Property HSV As HSV

    Public Function ToNative() As System.Drawing.Color
        Return System.Drawing.Color.FromArgb(
            Me.RGB.Red,
            Me.RGB.Green,
            Me.RGB.Blue)
    End Function

    Overloads Shared Narrowing Operator CType(ByVal original As Color) As System.Drawing.Color
        Return original.ToNative
    End Operator

    Public Overrides Function Equals(ByVal obj As Object) As Boolean
        If obj Is Me Then
            Return True
        End If

        Dim colorObject = TryCast(obj, Color)
        If colorObject IsNot Nothing Then
            Return colorObject.HTMLColorCode = Me.HTMLColorCode
        End If

        If TypeOf obj Is System.Drawing.Color Then
            Return Me.ToNative = CType(obj, System.Drawing.Color)
        End If

        Return MyBase.Equals(obj)
    End Function

    Overloads Shared Operator =(ByVal left As Color, ByVal right As Color)
        Return left.Equals(right)
    End Operator
    Overloads Shared Operator <>(ByVal left As Color, ByVal right As Color)
        Return Not left = right
    End Operator
End Class
